﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HKEA.CMS.Core.IDAL.Tbl;
using HKEA.CMS.Core.Models;
using HKEA.CMS.Core.Utility;
using HKEA.CMS.Core.DALFactory;
using System.Linq.Dynamic;
namespace HKEA.CMS.Core.DAL.Tbl
{
    public class tblAddressDAL:ItblAddress
    {
        HKEA_CMSEntities model;

        public tblAddressDAL()
        {
            model = DataAccess.GetModelInstance();
        }

        public static tblAddressDAL GetInstance()
        {
            return new tblAddressDAL();
        }

        public int InsertENT(tblAddress ent)
        {
            ent.CreationDate = DateTime.Now;
            ent.LastModifyDate = DateTime.Now;
            model.tblAddresses.AddObject(ent);
            model.SaveChanges();
            ent = model.tblAddresses.Where(a => a.AddressUID > 0).OrderBy(a => a.AddressUID).ToList().Last();
            return ent.AddressUID;
        }

        public List<tblAddress> GetListByENT(tblAddress ent, PageParam param, string strPropertyName,string strOrderBy)
        {
            var result = model.tblAddresses.Where(a => a.AddressUID > 0);
            if (ent.AddressUID > 0)
                result = result.Where(a => a.AddressUID == ent.AddressUID);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            param.Count = result.Count();
            result = result.OrderBy(strPropertyName + " " + strOrderBy).Skip((param.CurrentPage - 1) * param.PageSize).Take(param.PageSize);
            return result.ToList();
        }

        public List<tblAddress> GetListByENT(tblAddress ent)
        {
            var result = model.tblAddresses.Where(a => a.AddressUID > 0);
            if (ent.AddressUID > 0)
                result = result.Where(a => a.AddressUID == ent.AddressUID);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            return result.ToList();
        }

        public int GetCountByENT(tblAddress ent)
        {
            var result = model.tblAddresses.Where(a => a.AddressUID > 0);
            if (ent.AddressUID > 0)
                result = result.Where(a => a.AddressUID == ent.AddressUID);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            return result.Count();
        }

        public int UpdateENT(tblAddress ent)
        {
            var address = model.tblAddresses.Where(a=>a.AddressUID==ent.AddressUID).FirstOrDefault();
            if (address != null)
            {
                address.LastModifyBy = ent.LastModifyBy;
                address.LastModifyDate = DateTime.Now;
                address.Area_Chi = ent.Area_Chi;
                address.Area_Eng = ent.Area_Eng;
                address.City_Chi = ent.City_Chi;
                address.City_Eng = ent.City_Eng;
                address.CountryUID = ent.CountryUID;
                address.District_Chi = ent.District_Chi;
                address.District_Eng = ent.District_Eng;
                address.Flat_Bldg_Chi = ent.Flat_Bldg_Chi;
                address.Flat_Bldg_Eng = ent.Flat_Bldg_Eng; 
                address.FullAdd_Chi = ent.FullAdd_Chi;
                address.FullAdd_Eng = ent.FullAdd_Eng;
                address.PostalCode = ent.PostalCode;
                address.Province_Chi = ent.Province_Chi;
                address.Province_Eng = ent.Province_Eng;
                address.Street_Rd_Chi = ent.Street_Rd_Chi;
                address.Street_Rd_Eng = ent.Street_Rd_Eng;
            }
            return model.SaveChanges();
        }

        public int DeleteENT(tblAddress ent)
        {
            var temp = model.tblAddresses.Where(a => a.AddressUID > 0);
            if (ent.AddressUID > 0)
                temp = temp.Where(a=>a.AddressUID==ent.AddressUID);
            var list = temp.ToList();
            for (int i = 0; i < list.Count(); i++)
            {
                model.tblAddresses.DeleteObject(list[i]);
            }
            return model.SaveChanges();
        }
    }
}
